package Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    private  static  final  int ADDSTUDENT=1;
    private  static  final  int DELSTUDENT=2;
    private  static  final  int UPDATESTUDENT=3;
    private  static  final  int cxstudent=4;
    private  static  final  int Exit=5;
    public static void startStudentSystem() {
        ArrayList<Student> list = new ArrayList<Student>();
        while (true) {
            System.out.println("------------欢迎来到陈昊学生的管理系统---------------------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.println("请输入您的选项:");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
            switch (choice) {
                case ADDSTUDENT -> addStudent(list);
                case DELSTUDENT -> dealStudent(list);
                case UPDATESTUDENT -> updateStudent(list);
                case cxstudent -> cxStudent(list);
                case Exit -> {
                    System.out.println("退出");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项.请重新输入");
            }
        }
    }

    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        Student stu = new Student();
        System.out.println("请输入学生的id");
        String id = sc.next();
        boolean flag = contains(list, id);
        //id存在,直接回主界面
        if (flag) {
            System.out.println("id重复,请重新输入");
            return;
        } else {
            stu.setId(id);
        }

        System.out.println("请输入学生的的姓名");
        String name = sc.next();
        stu.setName(name);
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        stu.setAge(age);
        System.out.println("请输入学生的家庭地址");
        String Dz = sc.next();
        stu.setDz(Dz);
        //将对象添加在集合中
        list.add(stu);
    }

    public static void dealStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删的id");
        String id = sc.next();
        //查询id在集合中的索引
        int index = getIndex(list, id);
        //对index进行判断
        //如果大于等于0,表示存在,直接删除
        //如果-1,就表示不存在,结束方法,回到初始菜单
        if (index >= 0) {
            list.remove(index);
            System.out.println("id为:" + id + "删除成功");
        } else {
            System.out.println("id不存在,删除失败");

        }

    }

    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的id");
        String id = sc.next();
        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("要删除的id" + id + "不存在");

            return;
        }
        //id存在走这里
        //获取要修改的id信息
        Student stu = list.get(index);
        //输入其他信息并且修改
        System.out.println("请输入你的新名字");
        String newName = sc.next();
        stu.setName(newName);
        System.out.println("请输入你的新年龄");
        int age = sc.nextInt();
        stu.setAge(age);
        System.out.println("请输入你的新家庭地址");
        String Dz = sc.next();
        stu.setDz(Dz);

    }

    //判断id在集合中是否存在
    public static boolean contains(ArrayList<Student> list, String id) {
        return getIndex(list, id) >= 0;
    }

    //返回下标 看id在集合是否存在并且返回对应的索引
    public static int getIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String sid = stu.getId();
            if (sid.equals(id)) {
                return i;
            }
        }
        return -1;

    }

    //查询功能
    public static void cxStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前无学生信息,请添加后查询");
            return;
        }
        //到了这里有学生的信息了 开始打印格式
        System.out.println("id\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getClass());
        }
    }
}
